
$(function(){
	var getShareUrl = '',getUserInfo = '';
	localStorage.user_balance = localStorage.user_balance + localStorage.rec_money;
	/*获取答题结果数据localStorage.sub_result*/
	var sub_rel = JSON.parse(localStorage.sub_result);
	//console.log(sub_rel);
	/*剩余时间*/
	var res_time = sub_rel.object.secondsToCheck;
	$('.sub-res .fs-36').text(parseInt(res_time/60)+'分'+res_time%60+'秒');
	/*消耗n元*/
	$('.success-btn-exc em').eq(0).text(sub_rel.object.betMoney);
	$('.success-btn-exc em').eq(1).text(sub_rel.object.authorName);
	/*叉号和返回按钮失效解决办法*/
	$('.sub-close a,.btn-start a').attr('href',url() + '/htm/index.html?'+Math.random());
	/*再来一局*/
	$('.success-btn-agin').click(function(){
		if(sub_rel.object.againQuiz != '' && sub_rel.object.againQuiz != null){
			if(sub_rel.object.againQuiz.difficultLevel != 0){
				var obj = {
					Title:sub_rel.object.againQuiz.title,
					userName:sub_rel.object.againQuiz.authorName,
					usertitle:sub_rel.object.againQuiz.authorTitle,
					level:sub_rel.object.againQuiz.difficultLevel,
					time:sub_rel.object.againQuiz.allowedTime,
					couList:sub_rel.object.againQuiz.couponList,
					money:sub_rel.object.againQuiz.betMoney,
					categ:sub_rel.object.againQuiz.categories,
					uuid:sub_rel.object.againQuiz.uuid,
					id:sub_rel.object.againQuiz.categories[0].id
				};
				musval('start-cont',obj,0);
				$('.start').show();
				/*初始化弹窗事件*/
				coupon(obj.couList); 
			}else{
				var free_obj = {
					id:sub_rel.object.againQuiz.categories[0].id,
					uuid:sub_rel.object.againQuiz.uuid,
					money:0
				};
				startGame(free_obj);
			}
		}else{
			if(sub_rel.object.difficultLevel == 0){
				alert('天了撸！所有免费题都被您征服了，还不赶紧试试其他难度的题吧')
			}else{
				if(sub_rel.object.hasQuizInCurrCatFlg == 1){
					alert(sub_rel.object.catName+'中所有'+difficultLevel(sub_rel.object.difficultLevel)+'难度的题都被您征服了，去试试其他难度or其他领域的题吧')
				}else{
					alert(sub_rel.object.catName+'中所有的题都被您征服了，去试试其他领域的题吧')
				}
			}
		}	
	});

	/*其他题目*/
	if(sub_rel.object.suggestQuizs.length > 0){
		for(var i=0;i<sub_rel.object.suggestQuizs.length;i++){
			index(sub_rel.object.suggestQuizs[i].categories)
		};
		musval('oth-fie',sub_rel.object,0);
		$('.oth-fie > .col-xs-6').each(function(key){
			$(this).click(function(){
				var obj = {
					Title:sub_rel.object.suggestQuizs[key].title,
					userName:sub_rel.object.suggestQuizs[key].authorName,
					usertitle:sub_rel.object.suggestQuizs[key].authorTitle,
					level:sub_rel.object.suggestQuizs[key].difficultLevel,
					time:sub_rel.object.suggestQuizs[key].allowedTime,
					couList:sub_rel.object.suggestQuizs[key].couponList,
					money:sub_rel.object.suggestQuizs[key].betMoney,
					categ:sub_rel.object.suggestQuizs[key].categories,
					uuid:sub_rel.object.suggestQuizs[key].uuid,
					id:sub_rel.object.suggestQuizs[key].categories[0].id
				};
				musval('start-cont',obj,0);
				$('.start').show();
				coupon(obj.couList);
			})
		})
	}else{
		$('.sub-other-bal').hide();
	};
	/*弹层禁止滑动同时不影响优惠券的滑动*/
	$('.sta-bg')[0].addEventListener("touchmove",function(e){
		e.preventDefault();
	},true);
	/*隐藏弹层*/
	$('.sta-bg').click(function(){
		$('.start').hide();
	});

	/*答对付费看答案*/
	$('.success-btn-exc').click(function(){
		$.ajax({
			type:'post',
			url:url() + '/quiz/payForAnswer',
			data:{
				id:localStorage.quizInstanceId
			},
			dataType:'json',
			success:function(res,sta,xhr){
				var sessionstatus = xhr.getResponseHeader('redirectStatus');
				
				if(sessionstatus == '302'){
					window.location.href = url() + '/wxuser/goCompleteInfo';
					return false;
				};
				var res_obj = JSON.parse(res.result)
				//console.log(res_obj);
				if(res.code == 0 && res_obj.status == 0){
					/*答案显示*/

					localStorage.user_balance = localStorage.user_balance - localStorage.rec_money;
					
					musval('sub_ans',res_obj.object,1);
					$('.sub_ans strong').text(ChooseAns(res_obj.object.correctAnswer-1));
					for(var i=0;i<$('.sub_ans .row').length;i++){
						/*初始化选项样式*/
						if(i == (res_obj.object.correctAnswer-1)){
							$('.sub_ans .row').eq(i).addClass('col-8d').removeClass('col-f00');
							$('.sub_ans .row').eq(i).addClass('success-icon').removeClass('error-icon');
							var src = $('.sub_ans .row').eq(i).find('img').attr('src').replace('error','success');
							$('.sub_ans .row').eq(i).find('img').attr('src',src);
						}
					};
					/*专家解析*/
					musval('sub-bg',res_obj.object,0);
					if(sub_rel.object.analysisAttachments.length == 0 ){
						$('.sub-img-cont').hide()
					}
					$('.difficultLevel').each(function(){
						$(this).text(difficultLevel($(this).text()))
					});
					$('.sub-answer,.sub-analy').show();
					$('.success-btn-exc').parent().hide();
				}else{
					alert(res_obj.message)
				}
			},
			error:function(){
				console.log('error')
			}
		})
	});

	$('.difficultLevel').each(function(){
		$(this).text(difficultLevel($(this).text()))
	});
	/*分享提示*/
	$('.sub-share-click').click(function(){
		$('.sub-share').show();
	})
	/*分享弹层隐藏*/
	$('.sub-share').click(function(){
		$(this).hide()
	});
	/*分享禁止页面滑动*/
	$('.sub-share')[0].addEventListener('touchmove',function(e){e.preventDefault()},false);
	
	/*余额不足*/
	$('.balance-lack .yes').click(function(){
		window.location.href = url() + '/htm/recharge.html'
	});
	$('.balance-lack .no').click(function(){
		$('.balance-lack').hide();
	});
	$('.balance-lack')[0].addEventListener("touchmove",function(e){
		e.preventDefault();
	},true);

	
	/*//获取微信js-sdk配置参数
	$.ajax({
		type:'post',
		url:url() + "/wechat/getWxConfig",
		dataType:'json',
		success:function(result,sta,xhr) {
			var sessionstatus = xhr.getResponseHeader('redirectStatus');
			
			if(sessionstatus == '302'){
				window.location.href = url() + '/wxuser/goCompleteInfo';
				return false;
			};
			//console.log(JSON.parse(result.result));
			localStorage.wxconfig = JSON.stringify(result);
			var wxconfig = JSON.parse(localStorage.wxconfig);
			if(wxconfig.code ==0){
				var wxconfigResult = JSON.parse(wxconfig.result);
				if(wxconfigResult.status ==0) {
					var wxconfigObject = wxconfigResult.object;
					wx.config({
						debug: false,
						appId: wxconfigObject.appId,
						timestamp: wxconfigObject.timestamp,
						nonceStr: wxconfigObject.nonce,
						signature: wxconfigObject.sig,
						jsApiList: [
							'checkJsApi',
							'onMenuShareTimeline',
							'onMenuShareAppMessage',
							'onMenuShareQQ',
							'onMenuShareWeibo',
							'onMenuShareQZone'
						]
					});
				}else{
					alert(wxconfigResult.message);
				}
			}else{
				alert(wxconfig.result);
			}
		},
		error:function(){
			alert('error')
		}
	});

	分享地址
	$.ajax({
		type: 'post',
		url: url() + "/share/shareQuiz",
		data: {
			instanceId: localStorage.quizInstanceId
		},
		dataType: 'json',
		success: function (result,sta,xhr) {
			var sessionstatus = xhr.getResponseHeader('redirectStatus');
			
			if(sessionstatus == '302'){
				window.location.href = url() + '/wxuser/goCompleteInfo';
				return false;
			};
			//console.log(JSON.parse(result.result));
			var shareResult = JSON.parse(result.result);
			if(result.code ==0 && shareResult.status == 0) {
				getShareUrl = shareResult;
				//获取用户头像和昵称
				$.ajax({
					type:'get',
					url:url() + '/user/getUserInfo',
					data:{},
					dataType:'json',
					success:function(rel,sta,xhr){
						var sessionstatus = xhr.getResponseHeader('redirectStatus');
						
						if(sessionstatus == '302'){
							window.location.href = url() + '/wxuser/goCompleteInfo';
							return false;
						};
						var rel_obj = JSON.parse(rel.result);
						//console.log(rel_obj);
						localStorage.userInfo = JSON.stringify(rel_obj.object);
						if(rel.code == 0 && rel_obj.status == 0){
							getUserInfo = {
								'name':rel_obj.object.nickName,
								'img':rel_obj.object.headImg,
								'title':localStorage.now_title
							};
							console.log(getShareUrl)
							console.log(getUserInfo)
							wx.ready(function () {
								wx.showOptionMenu();
								wx.checkJsApi({
									jsApiList: [
										'onMenuShareTimeline',
										'onMenuShareAppMessage',
										'onMenuShareQQ',
										'onMenuShareWeibo',
										'onMenuShareQZone'
									],
									success: function (res) {
										console.log(getShareUrl)
										localStorage.checkObject = JSON.stringify(res);
										var checkObject = JSON.parse(localStorage.checkObject);
										if (checkObject.errMsg.lastIndexOf("ok") != -1) {
											var userInfo = getUserInfo;  //用户信息
											var title = userInfo.name+'邀您尝试疯狂奖学金1分钟1次的高潮体验 \\^o^/';
											var imgUrl = userInfo.img;
											var desc = userInfo.title;
											var link = getShareUrl.object;
											var checkResult = checkObject.checkResult;
											if (checkResult.onMenuShareTimeline) {//获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
												wx.onMenuShareTimeline({
													title: title, // 分享标题
													link: link, // 分享链接
													imgUrl: imgUrl, // 分享图标
													success: function () {
														// 用户确认分享后执行的回调函数
														//alert("分享成功");
													},
													cancel: function () {
														// 用户取消分享后执行的回调函数
														//alert("分享已取消");
													}
												});
											}
											if (checkResult.onMenuShareAppMessage) {//获取“分享给朋友”按钮点击状态及自定义分享内容接口
												wx.onMenuShareAppMessage({
													title: title, // 分享标题
													desc: desc, // 分享描述
													link: link, // 分享链接
													imgUrl: imgUrl, // 分享图标
													type: '', // 分享类型,music、video或link，不填默认为link
													dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
													success: function () {
														// 用户确认分享后执行的回调函数
														//alert("分享成功");

													},
													cancel: function () {
														// 用户取消分享后执行的回调函数
														//alert("分享已取消");

													}
												});
											}
											if (checkResult.onMenuShareQQ) {//获取“分享到QQ”按钮点击状态及自定义分享内容接口
												wx.onMenuShareQQ({
													title: title, // 分享标题
													desc: desc, // 分享描述
													link: link, // 分享链接
													imgUrl: imgUrl, // 分享图标
													success: function () {
														// 用户确认分享后执行的回调函数
														//alert("分享成功");

													},
													cancel: function () {
														// 用户取消分享后执行的回调函数
														//alert("分享已取消");

													}
												});
											}
											if (checkResult.onMenuShareWeibo) {//分享到腾讯微博
												wx.onMenuShareWeibo({
													title: title, // 分享标题
													desc: desc, // 分享描述
													link: link, // 分享链接
													imgUrl: imgUrl, // 分享图标
													success: function () {
														// 用户确认分享后执行的回调函数
														//alert("分享成功");

													},
													cancel: function () {
														// 用户取消分享后执行的回调函数
														//alert("分享已取消");

													}
												});
											}
											if (checkResult.onMenuShareQZone) {//分享到QQ空间
												wx.onMenuShareQZone({
													title: title, // 分享标题
													desc: desc, // 分享描述
													link: link, // 分享链接
													imgUrl: imgUrl, // 分享图标
													success: function () {
														// 用户确认分享后执行的回调函数
														//alert("分享成功");

													},
													cancel: function () {
														// 用户取消分享后执行的回调函数
														//alert("分享已取消");

													}
												});
											}
										} else {
											alert("当前客户端版本不支持从微信分享");
										}
									}
								});
							});
						}else{
							alert(rel_obj.message)
						}
						
					},
					error:function(){

					}
				});
			}else{
				alert(shareResult.message)
			}
		},
		error: function () {
			alert('error')
		}
	});*/

	
	
})

/*弹层事件*/
function coupon(obj){
	$('.start .difficultLevel').each(function(){
		$(this).text(difficultLevel($(this).text()));
	});
	$('.start button').click(function(){
		var star_obj = {
			id:$(".sta-cont").attr('data-id'),
			uuid:$(".sta-cont").attr('uuid'),
			money:$(".sta-cont").attr('bel')
		};
		startGame(star_obj);
	})
};

/*开始游戏*/
function startGame(obj){
	$.ajax({
		type:'post',
		url:url() + "/quiz/fetch2",
		data:{
			catId:obj.id,
			quizUuid:obj.uuid,
			betMoney:obj.money,
			couponId:coup
		},
		dataType:'json',
		success:function(result,sta,xhr) {
			var sessionstatus = xhr.getResponseHeader('redirectStatus');
			
			if(sessionstatus == '302'){
				window.location.href = url() + '/wxuser/goCompleteInfo';
				return false;
			};
			var res = JSON.parse(result.result)
			//console.log(res);
			if(result.code == 0 && res.status == 0){
				/*题目存储subject(更新localStorage.subject数据)*/
				localStorage.subject = result.result;
				/*当前题题目*/
				localStorage.now_title = res.object.title;
				/*分享地址参数id*/
				localStorage.quizInstanceId = res.object.quizInstanceId;
				localStorage.user_balance = localStorage.user_balance - obj.money;
				localStorage.rec_money = obj.money;
				
				window.location.href = url() + '/htm/subject.html'
			}else if(result.code == 0 && res.status == -12580){
				/*题目存储subject参数对象*/
				obj.coup = coup;
				localStorage.sub_canshu = JSON.stringify(obj);
				$('.start').hide();
				$('.balance-lack').show();
				localStorage.rec_money = obj.money;
				
			}else{
				alert(res.message);
			}
		},
		error:function(){
			console.log('error')
		}
	})
};